/**
 * Create a namespace for this application
 */
Ext.ns('App');

/*
 * Chinook Application Controller
 */
App.Chinook = {

    /**
     * This function performs create or update operations for window based editors.
     */
    createOrUpdateStoreRecord:function(editorForm,formHost,editorStore)
    {
        var formValues = editorForm.getForm().getValues();
        if( formHost.mode == 'edit')
        {
            var formValues = editorForm.getForm().getValues();
            formHost.record.beginEdit();
            for(var field in formValues)
            {
                formHost.record.set(field,formValues[field]);
            }
            formHost.record.endEdit();
            formHost.record.commit();
            formHost.hide();
        }
        else if( formHost.mode == 'create')
        {
            var newRecord = new editorStore.recordType(formValues);
            editorStore.add(newRecord);
            newRecord.commit();
            editorStore.reload();
            formHost.hide();
        }
    },

    /**
     * This function create a module object based ona menuitem
     */
    moduleConfigFromMenuItem:function(item)
    {
        return {
            id:item.id,
            iconCls:item.iconCls,
            url:App.Chinook.createURL('site',item.id),
            title:item.text
        }
    },

    /**
     * This function simply opens a new IFrame tab in the mainTabPanel
     * given a module configuration
     */
    activateModule:function(module)
    {        
        var tabid = '_tab'+module.id;
        var tab = Ext.getCmp(tabid);
        if(tab)
        {
            mainTabPanel.activate(tab);
            return;
        }

        tab = new Ext.Panel({
            id:tabid,
            title:module.title,
            autoScroll:true,
            layout:'fit',
            closable:true,
            iconCls:module.iconCls,
            autoLoad:
            {
                url:module.url,
                iframe:true,
                mask:{
                    msg:'Loading ' + module.title + '...'
                }
            }
        });

        mainTabPanel.add(tab);
        tab.doLayout();
        mainTabPanel.doLayout(tab);
        mainTabPanel.activate(tab);
    }

}